<?php
/**
 *
 * PHP API 绿色食品专区 控制器
 * @author jeff liu
 * powered by jeff liu 2011-10-14
 */
class Green_foodApp extends ApiApp
{

    function __construct()
    {
        $this->Green_foodApp();
    }

    function Green_foodApp()
    {
        parent::__construct();
    }

    /**
     * 完成抽奖结果并发送优惠券
     * powere by jeff liu 2011-10-14
     */
    function apiFinishLottery()
    {
        //added by jeff liu 2011-12-01 市场部提示不能再发送优惠券 直接返回错误start
        $ret_url = SITE_URL . '/index.php?app=my_coupon';
        $return_data = array(
            'code' => 1,
            'msg' => '抽奖活动已经结束！！！请联系相关人员。',
            'ret_url' => urlencode($ret_url)
        );
        $this->echo_result($return_data);
        exit;
        //added by jeff liu 2011-12-01 市场部提示不能再发送优惠券 直接返回错误end

        $umpid = trim($_REQUEST['umpid']);
        $order_sn = trim($_REQUEST['order_sn']);
        $pay_sn = trim($_REQUEST['pay_sn']);
        $lottery = $_REQUEST['lottery'];
        $description = trim($_REQUEST['description']) ? trim($_REQUEST['description']) : '';
        $key = trim($_REQUEST['key']);

        $calc_key = md5(
            'umpid=' . $umpid . '&order_sn=' . $order_sn . '&pay_sn=' . $pay_sn . '&lottery=' . $lottery . '&description=' . $description . '&skey=' . GREEN_FOOD_KEY
        );
        $coupon_sn = '';
        $user_id = 0;
        $msg = '';
        $code = 0;
        if (!empty($umpid)) { //根据umpid获得user_id
            $member_mod =& api_model('member');
            $user_id = $member_mod->get_user_id_by_umpid($umpid);
        } else {
            $msg = 'umpid missing';
            $code = 403;
        }

        if (empty($order_sn)) {
            $msg = 'order_sn missing';
            $code = 403;
        }
        if (empty($pay_sn)) {
            $msg = 'pay_sn missing';
            $code = 403;
        }
        if (empty($lottery)) {
            $msg = 'lottery missing';
            $code = 403;
        }
        if (empty($key)) {
            $msg = 'key missing';
            $code = 403;
        } else {
            if ($key != $calc_key) {
                $msg = 'key wrong';
                $code = 404;
            }
        }
        if (empty($user_id)) { //用户不存在
            $msg = 'user no exists';
            $code = 404;
        }
        //根据奖项获得对应的coupon_id
        $coupon_id = $this->_get_coupon_id_by_lottery($lottery);
        if (empty($coupon_id)) {
            $msg = 'coupon_id no exists';
            $code = 404;
        }
        //验证数据的有效性 start
        $verify_data = $this->_verify_finish_lottery($user_id, $order_sn, $pay_sn);
        if (!$verify_data) {
            $msg = 'data wrong';
            $code = 404;
        }
        //验证数据的有效性 end

        if (empty($msg)) { //没有出现错误信息
            //发放优惠券 start
            include_once 'lib/SendCouponSimple.php';
            $template = array(
                //站内信配置
                'message_template' => array(
                    'send' => true,
                    'template' => "tobuyer_get_coupon_notify",
                ),
                //邮件配置
                'mail_template' => array(
                    'send' => false,
                    'template' => "tobuyer_get_coupon_notify",
                ),
                //手机短信配置
                'mobile_message_template' => array(
                    'send' => false,
                    'template' => "tobuyer_sh_shopping_lottery_notify",
                ),
            );
            $coupon_send = new SendCouponSimple(
                array(
                    'coupon_id' => $coupon_id,
                    'login_verify' => false,
                    'user_id' => $user_id,
                ),
                $template
            );
            $rst = $coupon_send->send_coupon();
            $coupon_sn = $coupon_send->coupon_sn[$user_id];
            //发放优惠券 end

            //添加发放优惠券记录start
            $data = array(
                'user_id' => $user_id,
                'umpid' => $umpid,
                'order_sn' => $order_sn,
                'pay_sn' => $pay_sn,
                'coupon_id' => $coupon_id,
                'coupon_sn' => $coupon_sn,
                'lottery' => $lottery,
                'description' => $description,
                'add_time' => time(),
            );
            $goods_food_lucky_draw_log_mod =& api_model('green_food_lucky_draw_log');
            $goods_food_lucky_draw_log_mod->add($data);
            //添加发放优惠券记录end

            if (!$goods_food_lucky_draw_log_mod->has_error()) { //添加成功
                $code = 1;
                $msg = '优惠券发放成功';
            } else { //添加失败
                $code = 0;
                $msg = '优惠券发放失败';
            }
        }

        //查询优惠券的地址
        $ret_url = SITE_URL . '/index.php?app=my_coupon';
        $return_data = array(
            'code' => $code,
            'msg' => $msg,
            'ret_url' => urlencode($ret_url)
        );
        error_log(
            '$_REQUEST: ' . var_export($_REQUEST, 1) . PHP_EOL . 'return_data: ' . var_export(
                $return_data,
                1
            ) . PHP_EOL . PHP_EOL,
            3,
            SHARE_DATA_PATH . '/data/temp/apiFinishLottery.log'
        );
        $this->echo_result($return_data);
    }

    /**
     * 验证传递过来的数据是否正确
     * @param type $user_id
     * @param type $order_sn
     * @param type $pay_sn
     * @return type
     */
    private function _verify_finish_lottery($user_id, $order_sn)
    {
        $order_mod =& m('order');
        $rs = $order_mod->verify_finish_lottery($user_id, $order_sn);
        return $rs;
    }

    /**
     * 验证传递过来的信息是否属实
     * powered by jeff liu 2011-11-03
     */
    public function apiVerifyOrderInfo()
    {
        $order_sn = $_REQUEST['orderId']; //order_sn
        $umpid = $_REQUEST['umpid']; //umpid
        $order_date = $_REQUEST['orderDate']; //下单日期
        $order_amount = $_REQUEST['orderAmt']; //下单金额
        $mac = $_REQUEST['mac']; //验签

        $rs = false;
        $user_id = 0;
        $rs_code = '1001';
        $message = '';
        $final_msg = '';
        if (empty ($order_sn)) {
            $rs_code = '1002';
            $message .= 'orderId必填;';
        }
        if (empty($order_date)) {
            $rs_code = '1002';
            $message .= 'orderDate必填;';
        }
        if (empty($order_amount)) {
            $rs_code = '1002';
            $message .= 'orderAmt必填;';
        }
        if (empty($mac)) {
            $rs_code = '1002';
            $message .= 'mac必填;';
        } else {
            $calc_mac = md5($order_sn . $order_date . $order_amount . md5(GREEN_FOOD_KEY));
            if ($calc_mac != $mac) {
                $rs_code = '1002';
                $message .= 'mac错误;';
            }
        }
        if (empty($umpid)) {
            $rs_code = '1002';
            $message .= 'umpid必填;';
        } else { //传递了umpid过来，根据umpid获得对应的用户id
            $member_mod =& api_model('member');
            $user_id = $member_mod->get_user_id_by_umpid($umpid);
        }

        if (!empty($user_id) && $rs_code == '1002') { //获得了对应的用户ID
            $verify_data = $this->_verify_finish_lottery($user_id, $order_sn);
            if ($verify_data) {
                $final_msg = 'ResponseCode=1001&orderId=' . $order_sn . '&orderDate=' . $order_date . '&orderAmt=' . $order_amount . '&merId=value4&mac=' . md5(
                    '1001' . $order_sn . $order_date . $order_amount . 'value4' . GREEN_FOOD_KEY
                );
            }
        } else { //没有获得对应的用户ID
            $rs_code = '1002';
            $message .= 'umpid对应的用户不存在;';
            $final_msg = 'ResponseCode=' . $rs_code . '&Message=' . $message;
        }
        error_log(
            '$_REQUEST: ' . var_export($_REQUEST, 1) . PHP_EOL . 'final_msg: ' . $final_msg . PHP_EOL . PHP_EOL,
            3,
            SHARE_DATA_PATH . '/data/temp/apiVerifyOrderInfo.log'
        );
        $this->echo_result($final_msg);
    }

    /**
     * 根据给出来的奖项获得对应的coupon_id
     * powered by jefff liu 2011-10-14
     * @param type $lottery
     * @return type
     */
    protected function _get_coupon_id_by_lottery($lottery)
    {
        $coupon_id = defined('GREEN_FOOD_LD_COUPON_ID_' . $lottery) ? constant(
            'GREEN_FOOD_LD_COUPON_ID_' . $lottery
        ) : 0;
        return $coupon_id;
    }

}
